import {createRouter, createWebHistory} from 'vue-router'
import Layout from "@/component/Layout.vue";

const routers = [
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/',
        name: '首页',
        component: Layout,
        redirect: "/home", //重定向到home页面
        children: [
            {
                path: '/home',
                name: '首页',
                component: () => import('../views/Home.vue')
            },
            {
                path: '/message',
                name: '信息',
                component: () => import('../views/StudentMessage.vue')
            },
            {
                path: '/updateAward',
                name: '上传奖惩罚、竞赛信息',
                component:()=>import('../views/UpdateAward.vue')
            },
            {
                path: '/awardMessage',
                name: '奖惩信息',
            component:()=>import('../views/AwardMessage.vue')
            },
            {
                path: '/instructor',
                name: '辅导员',
                component:()=>import('../component/Instructor.vue')
            },
            {
                path: '/unit',
                name: '部门信息',
                component:()=>import('../views/Unit.vue')
            }       ,
            {
                path: '/grade',
                name: '成绩查询',
                component:()=>import('../component/Grade.vue')
            }
        ],
    },
    {
        path: '/information',
        component: () => import('../template/Information.vue')
    },
    {
        path: '/demo',
        component: () => import('../views/demo.vue')
    }
]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: routers
})

router.beforeEach((to, from, next) => {
    // 判断用户是否登录
    if (to.path !== '/login' && !localStorage.getItem('user')) {
        // 用户未登录，跳转到登录界面
        next('/login')
    } else {
        next()
    }
})

export default router
